package com.dw.dwdzcommerce.view.mainContent;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.conf.Constants;
import com.dw.dwdzcommerce.tools.commons.viewUtil.ViewUtil;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;

public class SelectLayout {

	private ViewGroup root;

	public void initModule(Fragment fragment) {
		root = (ViewGroup) LayoutInflater.from(fragment.getActivity()).inflate(R.layout.item_main_content_select, null);
		initLayout();

	}

	public void initModule(Context context) {
		root = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.item_main_content_select, null);
		initLayout();
	}

	@SuppressLint("NewApi")
	private void initLayout() {
		View layout = null;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			final GridLayout gridlayout = new GridLayout(root.getContext());

			gridlayout.setRowCount(1);
			gridlayout.setColumnCount(2);
			gridlayout.post(new Runnable() {
				@Override
				public void run() {
					// GridLayout.LayoutParams params=new
					// GridLayout.LayoutParams();
					// params.bottomMargin=5;
					// params.topMargin=5;
					// params.rightMargin=5;
					// params.leftMargin=5;

					GridLayout.LayoutParams params1 = new GridLayout.LayoutParams(
							new LayoutParams(ViewUtil.getWinWidth(root.getContext()) / 2, LayoutParams.WRAP_CONTENT));
					params1.bottomMargin = 5;
					params1.topMargin = 5;
					params1.rightMargin = 5;
					params1.leftMargin = 5;
					GridLayout.LayoutParams params2 = new GridLayout.LayoutParams(params1);
					View createChild1 = createChild(Constants.MY_FOCUS);
					View createChild2 = createChild(Constants.ARROUND);
					createChild1.setLayoutParams(params1);
					createChild2.setLayoutParams(params2);
					gridlayout.addView(createChild1);
					gridlayout.addView(createChild2);
				}
			});
			layout = gridlayout;
		} else {
			final LinearLayout linearlayout = new LinearLayout(root.getContext());
			linearlayout.setOrientation(LinearLayout.HORIZONTAL);
			linearlayout.post(new Runnable() {

				@Override
				public void run() {
					View createChild1 = createChild(Constants.MY_FOCUS);
					View createChild2 = createChild(Constants.ARROUND);
					LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
							new LayoutParams(ViewUtil.getWinWidth(root.getContext()) / 2, LayoutParams.WRAP_CONTENT));
					params1.bottomMargin = 5;
					params1.topMargin = 5;
					params1.rightMargin = 5;
					params1.leftMargin = 5;
					LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(params1);

					createChild1.setLayoutParams(params1);
					createChild2.setLayoutParams(params2);
					linearlayout.addView(createChild1);
					linearlayout.addView(createChild2);
				}
			});
			layout = linearlayout;
		}
		root.addView(layout);
	}

	private View createChild(final String text) {
		Button btn = new Button(root.getContext());
		btn.setText(text);
		btn.setBackgroundResource(R.drawable.btn_drawable);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onItemClcik(text);
				}
			}
		});

		return btn;
	}

	public View getView() {
		return root;
	}

	private OnItemClickListener mListener;

	public void setOnItemClickListener(OnItemClickListener mListener) {
		this.mListener = mListener;
	}

	public interface OnItemClickListener {
		void onItemClcik(String name);
	}

}
